package cn.taoshop.user.dao;

import java.util.List;
import java.lang.Number;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.taoshop.user.vo.User;
import cn.taoshop.utils.PageHibernateCallback;

// 用户模块持久层代码:
 
public class UserDao extends HibernateDaoSupport {

	// 按名次查询是否有该用户:
	public User findByUsername(String username) {
		String hql = "from User where username = ? ";
		List<User> list = this.getHibernateTemplate().find(hql, username);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	//查询另一用户名是否可用
	public User findByUser(User user){
		String hql="from User where username =? and uid != ?";
		List<User> list = this.getHibernateTemplate().find(hql,user.getUsername(),user.getUid());
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	// 注册用户存入数据库代码实现
	public void save(User user) {
		this.getHibernateTemplate().save(user);
	}

	// 根据激活码查询用户
	public User findByCode(String code) {
		String hql = "from User where code = ?";
		List<User> list = this.getHibernateTemplate().find(hql, code);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	// 修改用户状态的方法
	public void update(User existUser) {
		this.getHibernateTemplate().update(existUser);
	}
	
	//修改密码

	public void updatePwd(User user){
		
		
		this.getHibernateTemplate().update(user);
	}

	// 用户登录的方法
	public User login(User user) {
		String hql = "from User where username = ? and password = ? and state = ? and is_del = 0";
		List<User> list = this.getHibernateTemplate().find(hql,
				user.getUsername(), user.getPassword(), 1);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	// 查看用户激活状态
	public int checkuserstate(User user){
		String hql ="select state from User where username = ? and password = ? and is_del = 0";
		List<Integer> list=this.getHibernateTemplate().find(hql,
				user.getUsername(),user.getPassword());
		if(list != null && list.size() > 0){
			return list.get(0).intValue();
		}
		return -1;
	}
//检验邮箱是否注册
	public Integer checkEmail(String email,int state){
		
		String hql = "select count(*) from User where email =? and state = ? and is_del = 0";
		List list = this.getHibernateTemplate().find(hql,email,state);
		
		if(list != null && list.size() > 0){
			return ((Number) list.get(0)).intValue();
		}
		return -1;
	}
//查询另一邮箱是否注册
	public Integer checkEmailByUser(User user,int state){
		String hql = "select count(*) from User where email =? and state = ? and is_del = 0 and uid != ?";
		List list =this.getHibernateTemplate().find(hql,user.getEmail(),state,user.getUid());
		if(list != null && list.size() >0){
			return ((Number) list.get(0)).intValue();
		}
		return -1;
	}
	
	public int findCount() {
		String hql = "select count(*) from User where is_del = 0";
		List<Long> list = this.getHibernateTemplate().find(hql);
		if (list != null && list.size() > 0) {
			return list.get(0).intValue();
		}
		return 0;
	}

	public List<User> findByPage(int begin, int limit) {
		String hql = "from User where is_del = ?";
		List<User> list = this.getHibernateTemplate().execute(
				new PageHibernateCallback<User>(hql, new Object[]{0}, begin, limit));
		return list;
	}

	public User findByUid(Integer uid) {
		return this.getHibernateTemplate().get(User.class, uid);
	}

	public void delete(User existUser) {
		this.getHibernateTemplate().delete(existUser);
	}
}
